//
//  MyView.m
//  PetAPP
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "MyView.h"
#import "CellOfMyForOneSelect.h"
#import "CellOfMyForTwoSelect.h"
#import "MyHeaderOfCustomView.h"
#import "ModelOfUserinfo.h"
#import <YYWebImage.h>
#import "NetworkingHandle.h"

@interface MyView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIView *viewOfTableViewHeader;

@property (nonatomic, retain) UIImageView *imageViewOfFace;

@property (nonatomic, retain) UILabel *labelOfName;

@property (nonatomic, retain) UILabel *labelOfLevel;

@property (nonatomic, retain) UIButton *buttonOfMark;

@property (nonatomic, retain) ModelOfUserinfo *modelOfUser;

@end

@implementation MyView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0];

        [self createSubViewsWithFrame:(CGRect){.size = frame.size}];
        [self handleData];
    }
    
    return self;
}

- (void)handleData {

    [NetworkingHandle handleWorkData:@"http://bbs.chinapet.com/plugin.php?id=leepet_thread:api&action=getUserInfoByUid" AndBody:@"uid=1411934&version=d1bd83a33f1a841ab7fda32449746cc4" value:@{@"Content-Type" : @"application/x-www-form-urlencoded"} handleBlock:^(id result) {
       
        NSDictionary *tempDic = [result objectForKey:@"userinfo"];
        
        _modelOfUser = [[ModelOfUserinfo alloc] init];
        
        [self.modelOfUser setValuesForKeysWithDictionary:tempDic];
        [self changeHeaderViewContentWithModel:self.modelOfUser];
        
    }];
    

}



- (void)createSubViewsWithFrame:(CGRect)frame {
    
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    [self addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CellOfMyForTwoSelect class] forCellReuseIdentifier:@"poolOfCellForTwo"];
    [self.tableView registerClass:[CellOfMyForOneSelect class] forCellReuseIdentifier:@"poolOfCellForOne"];
    
    [self createHeaderViewWithFrame:frame];
    

}

- (void)createHeaderViewWithFrame:(CGRect)frame {

    self.viewOfTableViewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 130)];
    self.viewOfTableViewHeader.backgroundColor = [UIColor whiteColor];
    
    self.tableView.tableHeaderView = self.viewOfTableViewHeader;
    
    UIImageView *viewOfHeaderItem = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80, frame.size.width, 50)];
    viewOfHeaderItem.image = [UIImage imageNamed:@"MyItemView"];
    [self.viewOfTableViewHeader addSubview:viewOfHeaderItem];
    [viewOfHeaderItem release];
    viewOfHeaderItem.userInteractionEnabled = YES;
    
    for (int i = 0 ; i < 3; i++) {
        MyHeaderOfCustomView *view = [[MyHeaderOfCustomView alloc] initWithFrame:CGRectMake(i * (frame.size.width / 3), 0, frame.size.width / 3, 50)];
        [viewOfHeaderItem addSubview:view];
        [view release];
        view.tag = 1000 + i;
    }
    
    
    
    self.imageViewOfFace = [[UIImageView alloc] initWithFrame:CGRectMake(30, 15, 50, 50)];
    [self.viewOfTableViewHeader addSubview:self.imageViewOfFace];
    [_imageViewOfFace release];
    self.imageViewOfFace.layer.cornerRadius = 25;
    self.imageViewOfFace.layer.masksToBounds = YES;
    
    
    self.labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, 100, 20)];
    [self.viewOfTableViewHeader addSubview:self.labelOfName];
    [_labelOfName release];
    self.labelOfName.font = [UIFont systemFontOfSize:17];
    
    
    self.labelOfLevel = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 100, 15)];
    [self.viewOfTableViewHeader addSubview:self.labelOfLevel];
    [_labelOfLevel release];
    self.labelOfLevel.font = [UIFont systemFontOfSize:13];
    
    
    
    self.buttonOfMark = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.viewOfTableViewHeader addSubview:self.buttonOfMark];
    
    self.buttonOfMark.frame = CGRectMake(self.frame.size.width - 110, 30, 80, 30);
    self.buttonOfMark.layer.cornerRadius = 15;
    self.buttonOfMark.backgroundColor = [UIColor colorWithRed:1.0 green:0.9843 blue:0.7647 alpha:1.0];
    self.buttonOfMark.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.buttonOfMark setTitle:@"签到" forState:UIControlStateNormal];
    [self.buttonOfMark setTitleColor:[UIColor colorWithRed:0.3622 green:0.2404 blue:0.1197 alpha:1.0]forState:UIControlStateNormal];
    
    

}

- (void)changeHeaderViewContentWithModel:(ModelOfUserinfo *)model{
    self.imageViewOfFace.yy_imageURL = [NSURL URLWithString:model.picbig];
    
    self.labelOfName.text = model.username;
    
    self.labelOfLevel.text = [NSString stringWithFormat:@"Lv%@",model.level];
    
    if (model.follow == 0) {
        [self.buttonOfMark setTitle:@"已签到" forState:UIControlStateNormal];
        self.buttonOfMark.backgroundColor = [UIColor colorWithRed:0.9451 green:0.9451 blue:0.9451 alpha:1.0];
    }else {
        [self.buttonOfMark setTitle:@"签到" forState:UIControlStateNormal];
        self.buttonOfMark.backgroundColor = [UIColor colorWithRed:1.0 green:0.9843 blue:0.7647 alpha:1.0];
    
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 25)];
    view.backgroundColor = [UIColor colorWithRed:0.9625 green:0.9625 blue:0.9625 alpha:1.0];
    
    
    return view;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    return 5;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        CellOfMyForTwoSelect *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCellForTwo"];
        
        if (indexPath.section == 0) {
            cell.imageViewOfSelect.image = [UIImage imageNamed:@"MyItemOfMessage"];
        }else {
            cell.imageViewOfSelect.image = [UIImage imageNamed:@"MyItemOfPet"];
        }
        
        return cell;
    }else {
        CellOfMyForOneSelect *cell = [tableView dequeueReusableCellWithIdentifier:@"poolOfCellForOne"];
        switch (indexPath.section) {
            case 2:{
                cell.imageViewOfSelect.image = [UIImage imageNamed:@"MyItemOfFriend"];
                
                break;
            }
            case 3:{
                cell.imageViewOfSelect.image = [UIImage imageNamed:@"MyItemOfSet"];
                
                break;
            }
            case 4:{
                cell.imageViewOfSelect.image = [UIImage imageNamed:@"MyItemOfKefu"];
                
                break;
            }
        }
        return cell;
        
    
    
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1)
        return 110;
    else
        return 50;
}

@end
